const baseURL = getApp().globalData.api_url;

export default (params) => {
  let data = params.data || {};
  let header = {
    'content-type': 'application/json'
  }
  const token = wx.getStorageSync('token')
  if(token){
    header['Authorization'] = `Bearer ${token}`
  }
  return new Promise((resolve, reject) => {
    wx.request({
      url: baseURL + params.url,
      method: params.method || "GET",
      data,
      header,
      success: res => {
        if (res.data.code === 200) {
          resolve(res.data);
        } else {
          if(res.data.code === 401){
            wx.setStorageSync('token', '')
            wx.showToast({
              title: "用户未登录",
              icon: "error"
            })
            reject(res.data);
            return;
          }
          wx.showToast({
            title: res.data.msg,
            icon: "error"
          })
          reject(res.data);
        }
      },
      fail: err => {
        reject(err)
      }
    })
  })
}
